package pkg.drowne.org;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

public class Preferences extends Activity implements OnClickListener {
	
	// Preference file
	public static final String PREF_FILE = "pf2";
	
	private SharedPreferences preferences;
	
	private Button editButton;
	private EditText editNickname;
	private String nickname;
	private boolean answer;
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
	        WindowManager.LayoutParams.FLAG_FULLSCREEN,
	        WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        // creates the activity
        setContentView(R.layout.preferences);
        
        // Views
        registerViews();
        setupOnClickListeners();
        
        fillDataifExist();
  
    }
    
    private void fillDataifExist() {
    	
    	// check for the saved preferences  
        // get the preferences file ready
    	//preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
    	preferences = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
		
		nickname = preferences.getString("nickname", "");
		
		editNickname.setText(nickname);
		
	}
    
    private void registerViews() {

    	editButton = (Button) this.findViewById(R.id.btnEdit);
    	editNickname = (EditText) this.findViewById(R.id.edtNickname);
	
    }

	public void setupOnClickListeners() {
    	
		editButton.setOnClickListener(this);
    	
    }
	
	public void onClick(View v) {
		
		switch(v.getId()) {
			
		case R.id.btnEdit:
			// edit button pressed
			// save the data locally
			
			Utils.showToastShort(getBaseContext(), "Saving...");
		
			savePrefs();
			
			// TO-DO: change this in the server later
            
			break;
		}	
		
	}
		
	private boolean savePrefs() {

		// get the preferences file ready
		preferences = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
		//preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
		nickname = editNickname.getText().toString();
		
		// saves values in the preferences file
		preferences.edit().putString("nickname", nickname).commit();
		
		return true;
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.layout.erasedatamenu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    	case R.id.erasedata:
    		// call the confirmation dialog for the delete
    		
    		confirmationDialog();
    		
    		return true;
    	}
    return false;
    }
    
    private boolean confirmationDialog() {
		
		new AlertDialog.Builder(this)
			.setTitle(R.string.confirmationDelete)
			.setMessage(R.string.confirmationQuestion)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setPositiveButton(R.string.confirmationYES, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// YES CLICK	
					deleteAccount();
					dialog.dismiss();
					
					answer = true;
					
				}

			})

			.setNegativeButton(R.string.confirmationNO, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// CANCEL CLICK
					dialog.dismiss();
					
					answer = false;
				}
			}).show();
		
		return answer;
	}

	private void deleteAccount() {
		
		SharedPreferences.Editor editor = preferences.edit();
		
		editor.clear().commit();
		
		editor.putBoolean("first", true);
		editor.commit();
	
		finish();
		
	}

}
